package com.lwj.liwanjia.view.activity.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.UserBean;
import com.lwj.liwanjia.impl.UserView;
import com.lwj.liwanjia.presenter.UserPresenter;

/**
 * Created by 001 on 2017/9/5.
 */

public class ForgetActivity extends BaseActivity implements View.OnClickListener, UserView {

    private TextView tv_title;//头布局标题
    private TextView tv_back;//头布局左边的返回按钮
    private Button bt_next;
    private EditText et_number;//手机号码
    private UserPresenter presenter;
    private String number;

    @Override
    protected void initView() {

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_back = (TextView) findViewById(R.id.tv_left);
        bt_next = (Button) findViewById(R.id.bt_next);
        et_number = (EditText) findViewById(R.id.et_number);

        tv_back.setOnClickListener(this);
        bt_next.setOnClickListener(this);

        //初始化UI
        tv_back.setText("返回");
        tv_back.setVisibility(View.VISIBLE);
        tv_title.setText("忘记密码");

    }

    @Override
    protected void initData() {
        presenter = new UserPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回键
            case R.id.tv_left:
                finish();
                break;
            //下一步
            case R.id.bt_next:
                //获取手机号码
                //判断是注册号码还是新号码
                //注册号码就直接跳到修改密码
                number = et_number.getText().toString().trim();
                if (!number.matches("[1][34578]\\d{9}")) {
                    Toast.makeText(this, "手机号码格式不对", Toast.LENGTH_SHORT).show();
                    return;
                }
                presenter.getUserState(number);
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getUserState(UserBean userBean) {
        if (userBean.getState() == 0) {
            Toast.makeText(this, "接口有误", Toast.LENGTH_SHORT).show();
        } else if (userBean.getState() == 1) {
            if(userBean.getData()==1) {
                Intent intent = new Intent(ForgetActivity.this, ForgetSureActivity.class);
                intent.putExtra("Number", number);
                startActivity(intent);
                finish();
            }else if(userBean.getData()==0){
                Toast.makeText(this, "该账号不存在", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
